package cn.iocoder.yudao.module.handguard.service.houserecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.app.houserecord.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.houserecord.HouseRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.houserecord.HouseRecordConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.houserecord.HouseRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 用户-房子资产记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HouseRecordServiceImpl implements HouseRecordService {

    @Resource
    private HouseRecordMapper houseRecordMapper;

    @Override
    public Long createHouseRecord(HouseRecordCreateReqVO createReqVO) {
        // 插入
        HouseRecordDO houseRecord = HouseRecordConvert.INSTANCE.convert(createReqVO);
        houseRecordMapper.insert(houseRecord);
        // 返回
        return houseRecord.getId();
    }

    @Override
    public void updateHouseRecord(HouseRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateHouseRecordExists(updateReqVO.getId());
        // 更新
        HouseRecordDO updateObj = HouseRecordConvert.INSTANCE.convert(updateReqVO);
        houseRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteHouseRecord(Long id) {
        // 校验存在
        validateHouseRecordExists(id);
        // 删除
        houseRecordMapper.deleteById(id);
    }

    private void validateHouseRecordExists(Long id) {
        if (houseRecordMapper.selectById(id) == null) {
            throw exception(HOUSE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public HouseRecordDO getHouseRecord(Long id) {
        return houseRecordMapper.selectById(id);
    }

    @Override
    public List<HouseRecordDO> getHouseRecordList(Collection<Long> ids) {
        return houseRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<HouseRecordDO> getHouseRecordPage(HouseRecordPageReqVO pageReqVO) {
        return houseRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<HouseRecordDO> getHouseRecordList(HouseRecordExportReqVO exportReqVO) {
        return houseRecordMapper.selectList(exportReqVO);
    }

}
